﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
using System.Data;
 
using System.Web.UI.HtmlControls;

using Eis51.CampanyCMS.Business.Bus;
using Eis51.CampanyCMS.Model.Bus.QueryO;
using Eis51.CampanyCMS.Model;
using Eis51.CampanyCMS.FrameWork;
using Eis51.Web.Admin;

namespace Eis51.CampanyCMS.WebUI.Admin.LeaveMsg
{
    public partial class MsgList : BasePage
    {
        public string KeyType
        {
            get
            {
                if (Request.QueryString["KeyType"] != null)
                {
                    return Request.QueryString["KeyType"];
                }
                return "0";
            }
        }

        public string HaveNews
        {
            get
            {
                if (Request.QueryString["HaveNews"] != null)
                {
                    return Request.QueryString["HaveNews"];
                }
                return "-1";
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.rbtType.SelectedValue = this.KeyType;
                this.rbtHaveNews.SelectedValue = this.HaveNews;
                btnSearch_Click(null, null);
            }
        }
        

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            LeaveMsgMgntBLL lbll = LeaveMsgMgntBLL.GetInstance() ;
            LeaveMsgMgntQuery l = new LeaveMsgMgntQuery();
            l.Msg_Title = txtMsgTitle.Text;
            if (txtBeginDate.Text!=string.Empty)
            {
                l.Create_DateS = txtBeginDate.Text;
            }
            if (txtEndDate.Text != string.Empty)
            {
                l.Create_DateS = txtEndDate.Text;
            }
            l.UserName = AppContext.CurrentAdministrator.UserName;
            if (rbtType.SelectedValue!="0")
            {
                l.KeyType = rbtType.SelectedValue;
            }

            if (rbtHaveNews.SelectedValue!="-1")
            {
                l.havenews =int.Parse (rbtHaveNews.SelectedValue);
            }

            DataTable dt = lbll.SearchData(l);
            this.dgMsgs.Data = dt;
            this.dgMsgs.DataBind();

        }

        

        protected void dgMsgs_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Header)
            {
                CheckBox cbxAll = e.Item.FindControl("cbxAll") as CheckBox;
                if (cbxAll != null)
                    cbxAll.Attributes.Add("onclick", "javascript:return SelectAll('" + dgMsgs.ClientID + "');");
            }
            if (e.Item.ItemType == ListItemType.AlternatingItem ||
                e.Item.ItemType == ListItemType.Item)
            {
                Image imgIsnew = e.Item.FindControl("imgIsnew") as Image;
                imgIsnew.ImageUrl = "~/user/Img/oldmsg.gif";
                HtmlInputHidden txtMsg_ID = e.Item.FindControl("txtMsg_ID") as HtmlInputHidden;//imgIsnew
                if (txtMsg_ID != null)
                {
                    string Msg_ID = ComSafe.SafeValue(txtMsg_ID.Value);
                    Label lbCont = e.Item.FindControl("lbCont") as Label;
                    if (lbCont != null)
                    {
                        lbCont.Text = LeaveMsgMgntBLL.GetInstance().ReplyCount(int.Parse(Msg_ID)).ToString();
                    }
                }
                HtmlInputHidden txtHaveNews = e.Item.FindControl("txtHaveNews") as HtmlInputHidden;
                if (txtHaveNews.Value == LeaveMsgHaveNew.SellerHaveNew.ToString("d") || txtHaveNews.Value == LeaveMsgHaveNew.AllNew.ToString("d"))
                {
                    imgIsnew.ImageUrl = "~/user/Img/newmsg.gif";
                }
                Label lbKeyType = e.Item.FindControl("lbKeyType") as Label;
                lbKeyType.Text= LeaveMsgMgntBLL.GetInstance().GetLeaveMsgTypeCnName( lbKeyType.Text);    
            }
        }

        protected void dgMsgs_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            
            switch (e.CommandName)
            {
                case "View":
 
                    break;
                default:
                    break;
            }
        }

        protected void btndelete_Click(object sender, EventArgs e)
        {
            //Rlist.Items.Count
            for (int i = 0; i < dgMsgs.Items.Count; i++)
            {
                HtmlInputHidden txtMsg_ID = dgMsgs.Items[i].FindControl("txtMsg_ID") as HtmlInputHidden;//imgIsnew
                CheckBox chk = dgMsgs.Items[i].FindControl("cbItem") as CheckBox;// 
                if (chk.Checked)
                {
                    string Msg_ID = ComSafe.SafeValue(txtMsg_ID.Value);
                    LeaveMsgMgntBLL.GetInstance().Delete(int.Parse(Msg_ID));
                }
            }
            btnSearch_Click(null,null);
        }
    }
}
